package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.TermInfoMapper;
import cc.rengu.igas.bomp.common.entity.TermInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

public class TermInfoMapperImpl implements TermInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public TermInfo selectBompTermInfoByPrimaryKey(String instId, String mchntNo, String termNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompTermInfoByPrimaryKey");
        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(instId);
        termInfo.setMchntNo(mchntNo);
        termInfo.setTermNo(termNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, termInfo, termInfo);
        if (0 != iReturnCode) {
            rglog.error("根据主建获取终端信息失败,instId:<{}>,termNo:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, termNo, mchntNo, iReturnCode);
            return null;
        }
        return termInfo;
    }

    @Override
    public int updateBompTermInfoByPrimaryKey(TermInfo termInfo, String oprType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr;
        if ("A0".equals(oprType)) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompTermLongLatInfoByPrimaryKey");
        } else if ("00".equals(oprType)) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompTermBatchNoByPrimaryKey");
        } else if ("01".equals(oprType)) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompTermSignStatusByPrimaryKey");
        } else if ("02".equals(oprType)) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompKeyDownSignByPrimaryKey");
        } else if ("03".equals(oprType)) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompIcDownSignByPrimaryKey");
        } else if ("04".equals(oprType)) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompReserve2ByPrimaryKey");
        } else {
            rglog.error("未知道更新类型:{}", oprType);
            return -1;
        }
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, termInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新终端表状态信息失败,instId:<{}>,mchntNo:<{}>,termNo:<{}>,iReturnCode:<{}>", termInfo.getInstId(), termInfo.getMchntNo(), termInfo.getTermNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public TermInfo selectBompTermInfoByTermNo(String instId, String termNo, String termStatus) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompTermInfoByTermNo");
        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(instId);
        termInfo.setTermNo(termNo);
        termInfo.setTermStatus(termStatus);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, termInfo, termInfo);
        if (0 != iReturnCode) {
            rglog.error("获取终端信息失败,instId:<{}>,termNo:<{}>,termStatus:<{}>,iReturnCode:<{}>", instId, termNo, termStatus, iReturnCode);
            return null;
        }
        return termInfo;
    }

    @Override
    public List<TermInfo> selectBompTermInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompTermInfoByMchntNo");
        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(instId);
        termInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, termInfo, TermInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取终端信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (TermInfo) item).collect(Collectors.toList());
    }

    @Override
    public int updateBompAllTermStatusByMchntNo(TermInfo termInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBompAllTermStatusByMchntNo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, termInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新终端表状态信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", termInfo.getInstId(), termInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public TermInfo selectBompTermInfoNum(String instId, String mchntNo, String termStatus) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompTermInfoNum");
        TermInfo qrCodeInfo = new TermInfo();
        qrCodeInfo.setInstId(instId);
        qrCodeInfo.setMchntNo(mchntNo);
        qrCodeInfo.setTermStatus(termStatus);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, qrCodeInfo, qrCodeInfo);
        if (0 != iReturnCode) {
            rglog.error("获取二维码信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", instId, mchntNo, iReturnCode);
            return null;
        }
        return qrCodeInfo;
    }

    @Override
    public List<TermInfo> selectBompTermInfoByMchntNo(String instId, String mchntNo, String bindUserId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompTermInfoByMchntNo");
        TermInfo termInfo = new TermInfo();
        termInfo.setInstId(instId);
        termInfo.setMchntNo(mchntNo);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, termInfo, TermInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取终端信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (TermInfo) item).collect(Collectors.toList());
    }
}
